require(['../config'], function() {
  require(['jquery', 'avalon', 'util','Cookies','text!/common/nav.html','text!/common/footer.html','navInit'], function($, avalon, util,Cookies,
    navHtml,
    footerHtml,
    navInit) {

      $(".global-nav").html(navHtml);
    $(".global-footer").html(footerHtml);
    navInit();

    var vm = avalon.define({
      $id: 'product-list',
      data: {},
      pageItems: [1, 2, 3, 4, 5],
      params: {
        loanType: '',
        professional: '',
        loanAmount: '',
        loanTimeMax: '',
        payment: '',
        instType: '',
        hasServiceFee: '',
        orderBy: 0,
        page: 0,
        limit: 6
      },
      imgUrl: function(item) {
        return item.logo || 'http://img0.rongsue.com/images/channel/rec-logo.png'
      },
      productName: function(item) {
        return (item.instName || '') + '-' + item.name
      },
      tags: function(item) {
        var list = []
        var t = item.featuresText.split(',')
        $.each(item.features.split(','), function(i, str) {
          list.push('<span class="f' + str + '">' + t[i] + '</span>')
        })
        return list.join('')
      },
      info: function(item) {
        return (
          '贷款最高' +
          item.loanMax +
          '万元，' +
          item.takeTimesText +
          '内放款，利率' +
          item.rateMin +
          '%'
        )
      },
      desc: function(item) {
        return item.requirement
      },
      search: function() {
        var data = vm.$model
        util.ajax({
          url: '/loan/product/list',
          type: 'post',
          data: data.params,
          success: function(rsp) {
            vm.data = rsp.data
            vm.pageItems = util.getPageItems(
              rsp.data.pageNum,
              rsp.data.total,
              rsp.data.pages
            )
          }
        })
      },
      changePage: function(p) {
        this.params.page = p
        this.search()
      }
    })

    avalon.scan(document.body)

    $(function() {
      // 类型初始化
      var loanType = util.getUrlParam('loanType')
      if (loanType) {
        $('a[data-type="loanType"]').each(function() {
          var that = $(this)
          if (that.data('value') == loanType) {
            that.siblings().removeClass('active')
            that.addClass('active')
            vm.params.loanType = loanType
          }
        })
      }

      vm.search()
      // 查询处理
      $('.product-search a').click(function() {
        var that = $(this)
        that.siblings().removeClass('active')
        that.addClass('active')
        vm.params[that.data('type')] = that.data('value')
        // if you want search page to 1
        vm.params.page = 0
        vm.search()
      })
    })
  })
})
